package com.deyuanyun.pic.ae.controller;

import com.deyuanyun.pic.ae.controller.dto.AEBaseQuery;
import com.deyuanyun.pic.ae.controller.dto.GeologicHazardQuery;
import com.deyuanyun.pic.ae.controller.vo.GeologicHazardVO;
import com.deyuanyun.pic.ae.controller.vo.PopulationDistributionVO;
import com.deyuanyun.pic.ae.enumeration.AEExportEnum;
import com.deyuanyun.pic.ae.service.GeologicHazardService;
import com.deyuanyun.pic.ae.service.PopulationDistributionService;
import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “管道沿线人口分布” 管理控制类
 *
 * 方法详情：<br/>
 *
 * @author huanglw
 * @date  2016-11-12
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/ae/populationDistribution")
public class PopulationDistributionController extends AEBaseController {

    @Resource
    private PopulationDistributionService populationDistributionService;

    /**
     * 跳转到 “管道沿线人口分布” 管理页面
     *
     * @return
     */
    @RequestMapping("/index.html")
    public ModelAndView index(){
        return forword("/ae/populationDistribution");
    }

    /**
     * 条件 查询“管道沿线人口分布”
     *
     * @param aeBaseQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(AEBaseQuery aeBaseQuery) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(this.queryGeologicHazard(aeBaseQuery));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 条件 导出“管道沿线人口分布”
     *
     * @param aeBaseQuery
     */
    @RequestMapping("/export.json")
    @ResponseBody
    public void export(AEBaseQuery aeBaseQuery) throws IOException {
        List list = this.queryGeologicHazard(aeBaseQuery);
        AEExportEnum exportEnum = AEExportEnum.PopulationDistribution;
        String[] columnKeys = {"pipeLineName", "beginPile", "endPile", "province", "district",
                "district", "town", "village", "population", "contact",
                "contactPosition","telephone", "recorder", "recordTime", "remarks"};
        DownExcelUtil.export(exportEnum.getFileName(), aeTemplatePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
    }

    private Page<PopulationDistributionVO> queryGeologicHazard(AEBaseQuery aeBaseQuery){
        return new PopulationDistributionVO().fromDomainList(populationDistributionService.query(aeBaseQuery));
    }
}